package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.HomeworkInfoAnalyseScoreGraphService;

/**
 * 作业列表-写评语
 */
@CrossOrigin
@RestController
public class WorkRemarkGetController extends YxyApiBaseController {

    @Autowired
    HomeworkTaskReportRepository homeworkTaskReportRepository;

    @Autowired
    HomeworkInfoAnalyseScoreGraphService homeworkInfoAnalyseScoreGraphService;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/remark/get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestParam String taskId, //
            @RequestParam String classId, //
            ReqModel reqModel) throws ParseException {
        // ##
        // 获取用户基本信息
        try {
            reqModel.userId = getProfile(request, response).getId();
            reqModel.schoolId = getProfile(request, response).getSchoolId();
        } catch (Exception e) {
            Assert.isTrue(false, "用户信息异常，请重新登录。");
        }
        reqModel.taskId = taskId;
        reqModel.classId = classId;
        // ##
        List<HomeworkTaskReport> homeworkTaskReports = homeworkTaskReportRepository
                .findByTaskIdAndClassIdOrderByMyRankingAsc(reqModel.taskId, reqModel.classId);
        // 数据转化模型
        List<RespModel> respModels = new ArrayList<RespModel>();
        Integer types[] = { 0, 1, 2, 3, 4, -1 };
        // 循环输出数据
        for (Integer type : types) {
            RespModel respModel = new RespModel();
            respModel.type = type;
            respModel.students = this.getStudents(type, homeworkTaskReports);
            respModels.add(respModel);
        }
        return respModels;
    }

    /** 请求参数 */
    public static class ReqModel extends YxyApiReqModel {
        // 任务Id
        public String taskId;
        // 班级Id
        public String classId;
        // 用户Id (不用传参)
        public String userId;
        // 学校Id (不用传参)
        public String schoolId;
    }

    /** 响应 */
    public static class RespModel extends YxyApiRespModel {
        // 类型 0-全部, 1-90%~100%, 2-80%~90%, 3-60~80%, 4-<60%, -1-未交作业
        public Integer type;
        // 人数
        public List<Student> students;
    }

    public static class Student {
        // 学生Id
        public String studentId;
        // 学生名称
        public String studentNick;
    }

    /** 学生列表 */
    private List<Student> getStudents(Integer type, List<HomeworkTaskReport> homeworkTaskReports) {
        if (type == 0) {
            List<Student> students = new ArrayList<Student>();
            for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReports) {
                Student student = new Student();
                student.studentId = homeworkTaskReport.getStudentId();
                student.studentNick = homeworkTaskReport.getStudentNick();
                students.add(student);
            }
            return students;
        } else if (type == 1) {
            List<Student> students = new ArrayList<Student>();
            for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReports) {
                if (homeworkTaskReport.getRightRate() >= 90 && homeworkTaskReport.getRightRate() <= 100
                        && (homeworkTaskReport.getStatusOfFinished() == 0
                                || homeworkTaskReport.getStatusOfFinished() == 1
                                || homeworkTaskReport.getStatusOfFinished() == 2)
                        && !homeworkTaskReport.getTaskType().equals("1")) {
                    Student student = new Student();
                    student.studentId = homeworkTaskReport.getStudentId();
                    student.studentNick = homeworkTaskReport.getStudentNick();
                    students.add(student);
                }
            }
            return students;
        } else if (type == 2) {
            List<Student> students = new ArrayList<Student>();
            for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReports) {
                if (homeworkTaskReport.getRightRate() >= 80 && homeworkTaskReport.getRightRate() < 90
                        && (homeworkTaskReport.getStatusOfFinished() == 0
                                || homeworkTaskReport.getStatusOfFinished() == 1
                                || homeworkTaskReport.getStatusOfFinished() == 2)
                        && !homeworkTaskReport.getTaskType().equals("1")) {
                    Student student = new Student();
                    student.studentId = homeworkTaskReport.getStudentId();
                    student.studentNick = homeworkTaskReport.getStudentNick();
                    students.add(student);
                }
            }
            return students;
        } else if (type == 3) {
            List<Student> students = new ArrayList<Student>();
            for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReports) {
                if (homeworkTaskReport.getRightRate() >= 60 && homeworkTaskReport.getRightRate() < 80
                        && (homeworkTaskReport.getStatusOfFinished() == 0
                                || homeworkTaskReport.getStatusOfFinished() == 1
                                || homeworkTaskReport.getStatusOfFinished() == 2)
                        && !homeworkTaskReport.getTaskType().equals("1")) {
                    Student student = new Student();
                    student.studentId = homeworkTaskReport.getStudentId();
                    student.studentNick = homeworkTaskReport.getStudentNick();
                    students.add(student);
                }
            }
            return students;
        } else if (type == 4) {
            List<Student> students = new ArrayList<Student>();
            for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReports) {
                if (homeworkTaskReport.getRightRate() < 60
                        && (homeworkTaskReport.getStatusOfFinished() == 0
                                || homeworkTaskReport.getStatusOfFinished() == 1
                                || homeworkTaskReport.getStatusOfFinished() == 2)
                        && !homeworkTaskReport.getTaskType().equals("1")) {
                    Student student = new Student();
                    student.studentId = homeworkTaskReport.getStudentId();
                    student.studentNick = homeworkTaskReport.getStudentNick();
                    students.add(student);
                }
            }
            return students;
        } else if (type == -1) {
            List<Student> students = new ArrayList<Student>();
            for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReports) {
                if (homeworkTaskReport.getStatusOfFinished() == -1 || homeworkTaskReport.getStatusOfFinished() == 0
                        || homeworkTaskReport.getStatusOfFinished() == 3) {
                    Student student = new Student();
                    student.studentId = homeworkTaskReport.getStudentId();
                    student.studentNick = homeworkTaskReport.getStudentNick();
                    students.add(student);
                }
            }
            return students;
        }
        return new ArrayList<Student>();
    }
}
